{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## Import libraries"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from mealpy import FloatVar, BBO, Tuner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Define problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def objective_function(solution):\n",
    "    return np.sum(solution**2) + np.sum(solution**3)\n",
    "\n",
    "problem = {\n",
    "    \"bounds\": FloatVar(lb=(-10,)*20, ub=(10, )*20),    # 20 dimensions\n",
    "    \"obj_func\": objective_function,\n",
    "    \"minmax\": \"min\",\n",
    "    \"name\": \"Mixed Square and Cube Problem\",\n",
    "    \"log_to\": None,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Define model and parameter grid of the model (just like ParameterGrid / GridSearchCV)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "model = BBO.OriginalBBO()\n",
    "\n",
    "paras_bbo_grid = {\n",
    "    \"epoch\": [10, 20],\n",
    "    \"pop_size\": [20],\n",
    "    \"n_elites\": [2, 3, 4],\n",
    "    \"p_m\": [0.01, 0.02, 0.05, 0.1]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Define the Tuner "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuner = Tuner(model, paras_bbo_grid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Run the Tuner to find the best parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Algorithm: OriginalBBO, with params: {'epoch': 10, 'pop_size': 20, 'n_elites': 2, 'p_m': 0.01}, trial: 1, best fitness: -4883.570888741043\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (20,) (20,) (0,) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn [9], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m## Try to run this optimizer on this problem 3 times (n_trials = 3). \u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;66;03m## Get the best model by mean value of all trials\u001B[39;00m\n\u001B[1;32m----> 3\u001B[0m \u001B[43mtuner\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexecute\u001B[49m\u001B[43m(\u001B[49m\u001B[43mproblem\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mproblem\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_trials\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\tuner.py:401\u001B[0m, in \u001B[0;36mTuner.execute\u001B[1;34m(self, problem, termination, n_trials, n_jobs, mode, n_workers, verbose)\u001B[0m\n\u001B[0;32m    399\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    400\u001B[0m     \u001B[38;5;28;01mfor\u001B[39;00m idx \u001B[38;5;129;01min\u001B[39;00m trial_list:\n\u001B[1;32m--> 401\u001B[0m         idx, g_best, loss_epoch \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m__run__\u001B[49m\u001B[43m(\u001B[49m\u001B[43midx\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmode\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_workers\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mn_workers\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtermination\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtermination\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    402\u001B[0m         best_fit_results[\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m][trial_columns[idx]] \u001B[38;5;241m=\u001B[39m g_best\u001B[38;5;241m.\u001B[39mtarget\u001B[38;5;241m.\u001B[39mfitness\n\u001B[0;32m    403\u001B[0m         loss_results\u001B[38;5;241m.\u001B[39mappend(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m__generate_dict_result(params, idx, loss_epoch))\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\tuner.py:339\u001B[0m, in \u001B[0;36mTuner.__run__\u001B[1;34m(self, id_trial, mode, n_workers, termination)\u001B[0m\n\u001B[0;32m    338\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__run__\u001B[39m(\u001B[38;5;28mself\u001B[39m, id_trial, mode\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124msingle\u001B[39m\u001B[38;5;124m\"\u001B[39m, n_workers\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m, termination\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m--> 339\u001B[0m     g_best \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43malgorithm\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msolve\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mproblem\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmode\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_workers\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mn_workers\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtermination\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtermination\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    340\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39malgorithm\u001B[38;5;241m.\u001B[39mproblem\n\u001B[0;32m    341\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m id_trial, g_best, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39malgorithm\u001B[38;5;241m.\u001B[39mhistory\u001B[38;5;241m.\u001B[39mlist_global_best_fit\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\optimizer.py:237\u001B[0m, in \u001B[0;36mOptimizer.solve\u001B[1;34m(self, problem, mode, n_workers, termination, starting_solutions, seed)\u001B[0m\n\u001B[0;32m    234\u001B[0m time_epoch \u001B[38;5;241m=\u001B[39m time\u001B[38;5;241m.\u001B[39mperf_counter()\n\u001B[0;32m    236\u001B[0m \u001B[38;5;66;03m## Evolve method will be called in child class\u001B[39;00m\n\u001B[1;32m--> 237\u001B[0m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mevolve\u001B[49m\u001B[43m(\u001B[49m\u001B[43mepoch\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    239\u001B[0m \u001B[38;5;66;03m# Update global best solution, the population is sorted or not depended on algorithm's strategy\u001B[39;00m\n\u001B[0;32m    240\u001B[0m pop_temp, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mg_best \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mupdate_global_best_agent(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mpop)\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\bio_based\\BBO.py:91\u001B[0m, in \u001B[0;36mOriginalBBO.evolve\u001B[1;34m(self, epoch)\u001B[0m\n\u001B[0;32m     89\u001B[0m noise \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerator\u001B[38;5;241m.\u001B[39muniform(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mlb, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mub)\n\u001B[0;32m     90\u001B[0m condition \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerator\u001B[38;5;241m.\u001B[39mrandom(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mn_dims) \u001B[38;5;241m<\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mp_m\n\u001B[1;32m---> 91\u001B[0m pos_new \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mwhere\u001B[49m\u001B[43m(\u001B[49m\u001B[43mcondition\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnoise\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mpos_new\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     92\u001B[0m pos_new \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcorrect_solution(pos_new)\n\u001B[0;32m     93\u001B[0m agent_new \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerate_empty_agent(pos_new)\n",
      "File \u001B[1;32m<__array_function__ internals>:180\u001B[0m, in \u001B[0;36mwhere\u001B[1;34m(*args, **kwargs)\u001B[0m\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (20,) (20,) (0,) "
     ]
    }
   ],
   "source": [
    "## Try to run this optimizer on this problem 3 times (n_trials = 3). \n",
    "## Get the best model by mean value of all trials\n",
    "tuner.execute(problem=problem, n_trials=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## Can't run the parallel mode in Jupiter, but in Pycharm, Spider, or Command Line run just fine.\n",
    "\n",
    "* We can use sequential mode, which will take more times to run. (Default mode is also sequential)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Algorithm: OriginalBBO, with params: {'epoch': 10, 'pop_size': 20, 'n_elites': 2, 'p_m': 0.01}, trial: 1, best fitness: -5375.731816089305\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (20,) (20,) (0,) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn [10], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m## Try to run this optimizer on this problem 5 times (n_trials = 5). \u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;66;03m## Get the best model by mean value of all trials\u001B[39;00m\n\u001B[1;32m----> 3\u001B[0m \u001B[43mtuner\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexecute\u001B[49m\u001B[43m(\u001B[49m\u001B[43mproblem\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mproblem\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_trials\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m5\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;66;03m## Better to save the tunning results to CSV for later usage\u001B[39;00m\n\u001B[0;32m      6\u001B[0m tuner\u001B[38;5;241m.\u001B[39mexport_results()\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\tuner.py:401\u001B[0m, in \u001B[0;36mTuner.execute\u001B[1;34m(self, problem, termination, n_trials, n_jobs, mode, n_workers, verbose)\u001B[0m\n\u001B[0;32m    399\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    400\u001B[0m     \u001B[38;5;28;01mfor\u001B[39;00m idx \u001B[38;5;129;01min\u001B[39;00m trial_list:\n\u001B[1;32m--> 401\u001B[0m         idx, g_best, loss_epoch \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m__run__\u001B[49m\u001B[43m(\u001B[49m\u001B[43midx\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmode\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_workers\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mn_workers\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtermination\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtermination\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    402\u001B[0m         best_fit_results[\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m][trial_columns[idx]] \u001B[38;5;241m=\u001B[39m g_best\u001B[38;5;241m.\u001B[39mtarget\u001B[38;5;241m.\u001B[39mfitness\n\u001B[0;32m    403\u001B[0m         loss_results\u001B[38;5;241m.\u001B[39mappend(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m__generate_dict_result(params, idx, loss_epoch))\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\tuner.py:339\u001B[0m, in \u001B[0;36mTuner.__run__\u001B[1;34m(self, id_trial, mode, n_workers, termination)\u001B[0m\n\u001B[0;32m    338\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__run__\u001B[39m(\u001B[38;5;28mself\u001B[39m, id_trial, mode\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124msingle\u001B[39m\u001B[38;5;124m\"\u001B[39m, n_workers\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m, termination\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m--> 339\u001B[0m     g_best \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43malgorithm\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msolve\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mproblem\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmode\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn_workers\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mn_workers\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtermination\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtermination\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    340\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39malgorithm\u001B[38;5;241m.\u001B[39mproblem\n\u001B[0;32m    341\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m id_trial, g_best, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39malgorithm\u001B[38;5;241m.\u001B[39mhistory\u001B[38;5;241m.\u001B[39mlist_global_best_fit\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\optimizer.py:237\u001B[0m, in \u001B[0;36mOptimizer.solve\u001B[1;34m(self, problem, mode, n_workers, termination, starting_solutions, seed)\u001B[0m\n\u001B[0;32m    234\u001B[0m time_epoch \u001B[38;5;241m=\u001B[39m time\u001B[38;5;241m.\u001B[39mperf_counter()\n\u001B[0;32m    236\u001B[0m \u001B[38;5;66;03m## Evolve method will be called in child class\u001B[39;00m\n\u001B[1;32m--> 237\u001B[0m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mevolve\u001B[49m\u001B[43m(\u001B[49m\u001B[43mepoch\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    239\u001B[0m \u001B[38;5;66;03m# Update global best solution, the population is sorted or not depended on algorithm's strategy\u001B[39;00m\n\u001B[0;32m    240\u001B[0m pop_temp, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mg_best \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mupdate_global_best_agent(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mpop)\n",
      "File \u001B[1;32mE:\\github\\libs\\mealpy\\mealpy\\bio_based\\BBO.py:91\u001B[0m, in \u001B[0;36mOriginalBBO.evolve\u001B[1;34m(self, epoch)\u001B[0m\n\u001B[0;32m     89\u001B[0m noise \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerator\u001B[38;5;241m.\u001B[39muniform(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mlb, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mub)\n\u001B[0;32m     90\u001B[0m condition \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerator\u001B[38;5;241m.\u001B[39mrandom(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mproblem\u001B[38;5;241m.\u001B[39mn_dims) \u001B[38;5;241m<\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mp_m\n\u001B[1;32m---> 91\u001B[0m pos_new \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mwhere\u001B[49m\u001B[43m(\u001B[49m\u001B[43mcondition\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnoise\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mpos_new\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     92\u001B[0m pos_new \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcorrect_solution(pos_new)\n\u001B[0;32m     93\u001B[0m agent_new \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerate_empty_agent(pos_new)\n",
      "File \u001B[1;32m<__array_function__ internals>:180\u001B[0m, in \u001B[0;36mwhere\u001B[1;34m(*args, **kwargs)\u001B[0m\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (20,) (20,) (0,) "
     ]
    }
   ],
   "source": [
    "## Try to run this optimizer on this problem 5 times (n_trials = 5). \n",
    "## Get the best model by mean value of all trials\n",
    "tuner.execute(problem=problem, n_trials=5)\n",
    "\n",
    "## Better to save the tunning results to CSV for later usage\n",
    "tuner.export_results()\n",
    "\n",
    "## Also better to draw the figures that are available.\n",
    "tuner.export_figures()\n",
    "\n",
    "## Print out the best pameters\n",
    "print(f\"Best parameter: {tuner.best_params}\")\n",
    "\n",
    "## Print out the best score of the best parameter\n",
    "print(f\"Best score: {tuner.best_score}\")\n",
    "\n",
    "## Print out the algorithm with the best parameter\n",
    "print(f\"Best Optimizer: {tuner.best_algorithm}\")\n",
    "\n",
    "\n",
    "## Now we can even re-train the algorithm with the best parameter by calling resolve() function \n",
    "## Resolve() function will call the solve() function in algorithm with default problem parameter is removed. \n",
    "##    other parameters of solve() function is keeped and can be used.\n",
    "\n",
    "g_best = tuner.resolve()\n",
    "print(f\"Best solution after re-solve: {g_best.solution}\")\n",
    "print(f\"Best fitness after re-solve: {g_best.target.fitness}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}